package com.xinruke.hostels.common.service;

import com.xinruke.hostels.common.cache.service.RedisService;
import com.xinruke.hostels.common.constants.RedisConstants;
import com.xinruke.hostels.common.util.JwtUtil;
import com.xinruke.hostels.common.vo.AccountLoginVo;
import com.xinruke.hostels.common.vo.BusinessLoginVo;
import com.xinruke.hostels.common.cache.vo.LoginManageResultVO;
import com.xinruke.hostels.common.cache.vo.LoginReservationResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

@Service
@Transactional
public class CustomerService {

    @Resource
    private RedisService redisService;

    public AccountLoginVo getAccountInfo(String token) {
        String loginCode = JwtUtil.getLoginCode(token);
        if (StringUtils.isEmpty(loginCode)) {
            return null;
        }
        AccountLoginVo loginVo = new AccountLoginVo();
        LoginReservationResultVO customer = (LoginReservationResultVO) redisService.getObject(RedisConstants.RESERVATION_LOGIN_TOKEN_KEY_PREFIX + loginCode);
        if (customer != null) {
            BeanUtils.copyProperties(customer, loginVo);
        }
        return loginVo;
    }


    public BusinessLoginVo getBusinessAccount(String token) {
        String loginCode = JwtUtil.getLoginCode(token);
        if (StringUtils.isEmpty(loginCode)) {
            return null;
        }
        BusinessLoginVo loginVo = new BusinessLoginVo();
        LoginManageResultVO businessAccount = (LoginManageResultVO) redisService.getObject(RedisConstants.MANAGE_LOGIN_TOKEN_KEY_PREFIX + loginCode);
        if (businessAccount != null) {
            BeanUtils.copyProperties(businessAccount, loginVo);
        }
        return loginVo;
    }

//    public CustomerVo getCustomer(int zid) {
//        return customerMapper.get(zid + "");
//    }


}
